package com.qk.management.statemachine.listener;

import com.qk.constant.ClueConstant;
import com.qk.entity.Clue;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.statemachine.event.ClueEvent;
import com.qk.management.statemachine.status.ClueStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;
import org.springframework.statemachine.transition.Transition;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
@RequiredArgsConstructor
public class ClueStateChangeListener extends StateMachineListenerAdapter<ClueStatus, ClueEvent> {

    private final ClueMapper clueMapper;

    @Override
    public void transition(Transition<ClueStatus, ClueEvent> transition) {
        // 监听所有转换
    }

    @Override
    public void stateChanged(State<ClueStatus, ClueEvent> from, State<ClueStatus, ClueEvent> to) {
        System.out.println("状态变更: " + (from != null ? from.getId() : "null") + " -> " + to.getId());
        // 当状态发生变化时，此方法会被调用
        handleStateChange(from, to);
    }

    @Override
    public void eventNotAccepted(Message<ClueEvent> event) {
        // 事件不被接受时的处理逻辑
        System.err.println("事件不被接受: " + event.getPayload());
    }

    @Override
    public void stateContext(StateContext<ClueStatus, ClueEvent> context) {
        // 更细粒度的状态上下文处理
        if (context.getStage() == StateContext.Stage.STATE_ENTRY) {
            // 状态进入时的处理
            handleStateEntry(context);
        }
    }

    private void handleStateChange(State<ClueStatus, ClueEvent> from, State<ClueStatus, ClueEvent> to) {
        StateContext<ClueStatus, ClueEvent> context = getCurrentStateContext();
        if (context != null) {
            Map<String, Object> headers = context.getMessageHeaders();
            Integer clueId = (Integer) headers.get("clueId");

            if (clueId != null) {
                Integer targetStatus = mapStateToStatus(to.getId());
                if (targetStatus != null) {
                    updateClueStatus(clueId, targetStatus);
                }
            }
        }
    }

    private void handleStateEntry(StateContext<ClueStatus, ClueEvent> context) {
        Map<String, Object> headers = context.getMessageHeaders();
        ClueStatus state = context.getTarget().getId();
        Integer clueId = Integer.parseInt(Objects.requireNonNull(headers.get("clueId")).toString());
        // 根据目标状态执行不同的业务逻辑
        switch (state) {
            case ASSIGNED:
                // 分配后的处理逻辑
                updateClueStatus(clueId, ClueConstant.CLUE_STATUS_ASSIGNED);
                break;
            case FOLLOWING:
                // 开始跟进的处理逻辑
                updateClueStatus(clueId, ClueConstant.CLUE_STATUS_FOLLOWING);
                break;
            case PSEUDO_CLUES:
                // 标记为伪线索的处理逻辑
                updateClueStatus(clueId, ClueConstant.CLUE_STATUS_PSEUDO_CLUES);
                break;
            case BUSINESS:
                // 转为商机的处理逻辑
                updateClueStatus(clueId, ClueConstant.CLUE_STATUS_BUSINESS);
                break;
            default:
                break;
        }
    }


    private Integer mapStateToStatus(ClueStatus state) {
        return switch (state) {
            case WAITING -> ClueConstant.CLUE_STATUS_WAITING;
            case ASSIGNED -> ClueConstant.CLUE_STATUS_ASSIGNED;
            case FOLLOWING -> ClueConstant.CLUE_STATUS_FOLLOWING;
            case PSEUDO_CLUES -> ClueConstant.CLUE_STATUS_PSEUDO_CLUES;
            case BUSINESS -> ClueConstant.CLUE_STATUS_BUSINESS;
            default -> null;
        };
    }

    private void updateClueStatus(Integer clueId, Integer status) {
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setStatus(status);
        clue.setUpdateTime(LocalDateTime.now());
        clueMapper.updateById(clue);
    }

    // 辅助方法，用于获取当前状态上下文
    private StateContext<ClueStatus, ClueEvent> getCurrentStateContext() {
        return null; // 需要根据实际情况实现
    }
}